//
//  DDCancelOrderReasonView.m
//  Dingdong
//
//  Created by 程强 on 16/12/20.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDCancelOrderReasonView.h"

@interface DDCancelOrderReasonView()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *reasonTable;
@property (strong, nonatomic) NSString *selectReason;
@property (strong, nonatomic) NSIndexPath *lastIndex;
@end

@implementation DDCancelOrderReasonView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *backView = [UIButton buttonWithType:UIButtonTypeCustom];
        backView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        backView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [backView addTarget:self action:@selector(removeReasonView:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backView];
        
        UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, 400)];
        contentView.backgroundColor = [UIColor whiteColor];
        [backView addSubview:contentView];
        
        KKLabel *titleLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"Cancal Order",[UIColor grayColor],[UIFont systemFontOfSize:15.0],NSTextAlignmentCenter).laFrame(CGRectMake(0, 0, frame.size.width, 40));
        }];
        [contentView addSubview:titleLbl];
        
        UIButton *closeBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        [closeBtn setBackgroundImage:LoadImage(@"login_close") forState:UIControlStateNormal];
        [closeBtn setFrame:CGRectMake(frame.size.width-30, 12, 16, 16)];
        [closeBtn addTarget:self action:@selector(removeReasonView:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:closeBtn];
        
        UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, frame.size.width, 310) style:UITableViewStyleGrouped];
        table.delegate = self;
        table.dataSource = self;
        [contentView addSubview:table];
        self.reasonTable = table;
        
        UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [commitBtn setTitle:@"Submit" forState:UIControlStateNormal];
        [commitBtn setFrame:CGRectMake(0, 350, frame.size.width, 50)];
        commitBtn.backgroundColor = RGBColor(255, 80, 80);
        [commitBtn addTarget:self action:@selector(commitReason:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:commitBtn];
        
        self.alpha = 0;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.alpha = 1.0;
            contentView.frame = CGRectMake(0, frame.size.height-400, frame.size.width, 400);
        } completion:^(BOOL finished) {
            
        }];
        self.selectReason = @"";
    }
    return self;
}
- (void)removeReasonView:(UIButton *)sender
{
    [self removeFromSuperview];
}
- (void)commitReason:(UIButton *)sender
{
    if (self.selectReason.length != 0) {
        if (self.reasonBlock) {
            self.reasonBlock(self.selectReason);
            [self removeReasonView:nil];
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.reasonAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44.0)];
    view.backgroundColor = [UIColor whiteColor];
    KKLabel *titleLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
        lbl.laText(@"Please select Cancel Reason:",[UIColor blackColor],[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft).laFrame(CGRectMake(20, 0, ScreenWidth-20, 44));
    }];
    titleLbl.numberOfLines = 0;
    [view addSubview:titleLbl];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"ReasonCell";
    
    DDCancelReasonCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[DDCancelReasonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.reasonLbl.text = self.reasonAry[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.lastIndex) {
        DDCancelReasonCell *lastCell = [tableView cellForRowAtIndexPath:self.lastIndex];
        lastCell.selectIcon.highlighted = NO;
    }
    DDCancelReasonCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectIcon.highlighted = YES;
    self.selectReason = self.reasonAry[indexPath.row];
    self.lastIndex = indexPath;
}
- (void)userSelectCancelReason:(ChoseReasonHandler)handler
{
    self.reasonBlock = handler;
}
@end


@implementation DDCancelReasonCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectIcon = [[UIImageView alloc]initWithImage:LoadImage(@"cart_unselect")];
        [_selectIcon setHighlightedImage:LoadImage(@"common_select")];
        [self.contentView addSubview:_selectIcon];
        [_selectIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(20);
            make.width.height.equalTo(@20);
        }];
        
        self.reasonLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",[UIColor darkGrayColor],[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [self.contentView addSubview:_reasonLbl];
        [_reasonLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(_selectIcon.mas_right).offset(15);
            make.right.equalTo(self.contentView).offset(-20);
            make.height.equalTo(@30);
        }];
    }
    return self;
}
@end
